#include "ipdialog.h"
#include "ui_ipdialog.h"
#include "Camera/cameramanager.h"
#include "GenICam/GigE/GigECamera.h"
#include "GenICam/GigE/GigEInterface.h"
#include <sstream>
#include <QDebug>
#include "toast.h"
using namespace Dahua;
using namespace Dahua::GenICam;

IpDialog::IpDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IpDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowCloseButtonHint | Qt::MSWindowsFixedSizeDialogHint | Qt::Dialog);
    InitIpAddress();
}

void IpDialog::InitIpAddress()
{
    CameraManager *m = CameraManager::getInstance();
	ICameraPtr c = m->GetCurrentCamera();
	if (!c)
	{
		Toast *t = new Toast(this, tr("No Camera"), 180, 30);
		t->toast();
		QTimer *m_pTimer = new QTimer(this);
		m_pTimer->setSingleShot(true);

		connect(m_pTimer, &QTimer::timeout, this, [=]() {this->close(); });
		m_pTimer->start(1000);
		return;
	}
    Dahua::GenICam::IGigECameraPtr cameraPtr = Dahua::GenICam::IGigECamera::getInstance(c);
    Infra::CString ipAddr, mask, gateway;
    if (NULL == cameraPtr)
    {
        // MessageBox(L"Get Camera Failed.", L"", 0);
        return;
    }
    ipAddr = cameraPtr->getIpAddress();
    mask = cameraPtr->getSubnetMask();
    gateway = cameraPtr->getGateway();

    Dahua::GenICam::IGigEInterfacePtr interfacePtr = Dahua::GenICam::IGigEInterface::getInstance(c);
    Infra::CString interfaceIpAddr, interfaceMask, interfaceGateway;
    if (NULL == interfacePtr)
    {
        //  MessageBox(L"Get Interface Failed.", L"", 0);
        return;
    }
    interfaceIpAddr = interfacePtr->getIpAddress();
    interfaceMask = interfacePtr->getSubnetMask();
    interfaceGateway = interfacePtr->getGateway();

    Infra::CString disIPAddr;
//    std::string s1 = mask.c_str();
//    std::string s2 = interfaceMask.c_str();
//    std::string s3 = gateway.c_str();
//    std::string s4 = interfaceGateway.c_str();
    if (mask != interfaceMask || gateway != interfaceGateway)
    {
        std::string tempIp = interfaceIpAddr.c_str();
        std::replace(tempIp.begin(), tempIp.end(), '.', ' ');
        std::stringstream ipStrStream(tempIp);
        std::vector<std::string> ipVec;
        std::copy(std::istream_iterator<std::string>(ipStrStream),
                  std::istream_iterator<std::string>(),
                  std::back_inserter(ipVec));

        // 获取子网掩码的Vector
        std::string tempSubnet = mask.c_str();
        std::replace(tempSubnet.begin(), tempSubnet.end(), '.', ' ');
        std::stringstream subNetStrStream(tempSubnet);
        std::vector<std::string> subNetVec;
        std::copy(std::istream_iterator<std::string>(subNetStrStream),
                  std::istream_iterator<std::string>(),
                  std::back_inserter(subNetVec));

        assert(4 == subNetVec.size() && 4 == ipVec.size());

        std::stringstream disStringStream;
        for (int i = 0; i < 4; ++i)
        {
            int tempIp, tempSubnet;

            std::stringstream ss;
            ss << subNetVec[i];
            ss >> tempSubnet;

            std::stringstream ss1;
            ss1 << ipVec[i];
            ss1 >> tempIp;

            disStringStream << (tempIp & tempSubnet);
            if (i < 3)
            {
                disStringStream << ".";
            }
        }
        disIPAddr = disStringStream.str().c_str();
    }
    else
    {
        disIPAddr = ipAddr;
    }

    ui->widget->settext(disIPAddr.c_str());

    ui->widget_2->settext(interfaceMask.c_str());

    ui->widget_3->settext(interfaceGateway.c_str());

    connect(ui->pushButton, &QPushButton::clicked, [=](){
        QString ip = ui->widget->text();
        QString subnet = ui->widget_2->text();
        QString gateway = ui->widget_3->text();

        if (!cameraPtr->forceIpAddress(ip.toStdString().c_str(), subnet.toStdString().c_str(), gateway.toStdString().c_str()))
        {

        }
        else {
            this->close();
         //   qDebug()<<"ffffffffff";
        }
    });

}

IpDialog::~IpDialog()
{
    delete ui;
}
